﻿using System;
using System.Collections.Generic;
 
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using ExtAspNet;
using System.Text;

namespace HY.WEB.SA
{
    public partial class schoolpubcity : PageBase
    {

        public string whe = "",whe2="";
        public string us,um,ubh;
        public string tab = "school";
        public int pgCount = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            us = Session["ID"].ToString();
            um = Session["user"].ToString();
            ubh = Session["bh"].ToString();

            if (ViewState["tab"] != null)
            {
                if (ViewState["tab"].ToString() != "")
                    tab = ViewState["tab"].ToString();
            }
            else
                tab = "school";


            if (ViewState["whe"] != null)
            {
                if (ViewState["whe"].ToString() != "")
                    whe = ViewState["whe"].ToString();
            }
            else
                whe = "";

            if (ViewState["whe2"] != null)
            {
                if (ViewState["whe2"].ToString() != "")
                    whe2 = ViewState["whe2"].ToString();
            }
            else
                whe2 = "";


            pgCount = 0;
            Grid1.RecordCount = BLL.Bllbase.page_count(tab, whe2);
            pgCount = Grid1.RecordCount / Grid1.PageSize;
            if (Grid1.RecordCount % Grid1.PageSize != 0) pgCount += 1;

            lblcount.Text = "总记录:" + Grid1.RecordCount.ToString();

            if (!IsPostBack)
            {
                if (tab == "") tab = "school";
                    btnDelete.OnClientClick = Grid1.GetNoSelectionAlertReference("至少选择一项！");
                // btnSearch.OnClientClick =  Window1.GetShowReference("../function/search.aspx?tab="+tab+"", "高级搜索");
      
               // BindGrid();
                fill(); //填充查询
                pageset();
            }

        }

        //页面设置
        void pageset()
        {
            Grid1.RecordCount = BLL.Bllbase.page_count(tab, whe2);

            pgCount = Grid1.RecordCount / Grid1.PageSize;
            if (Grid1.RecordCount % Grid1.PageSize != 0)
            {
                pgCount += 1;
            }
            Grid1.PageCount = pgCount;
            Grid1.PageIndex = 1;

            txtgo.Text = Grid1.PageIndex.ToString();
            lblpgid.Text = "当前页:" + Grid1.PageIndex.ToString();
            lblpgct.Text = "总页数:" + pgCount.ToString();
            lblcount.Text = "总记录:" + Grid1.RecordCount.ToString();
        }


          private void fill()
          {
              // DataTable d = BLL.BLLfun.get_search(tab);
              ddlfiled.Items.Clear();

              DataTable d = BLL.Bllbase.get_table("pub_area", "", "order by svalue desc");
              for (int i = 0; i < d.Rows.Count; i++)
              {
                  ddlfiled.Items.Add(new ExtAspNet.ListItem(d.Rows[i][2].ToString().Replace("省", ""), d.Rows[i][2].ToString().Replace("省", "")));
              }
              ddlfiled.DataBind();
              ddlfiled.Items.Add(new ExtAspNet.ListItem("", ""));
              ddlfiled.SelectedIndex = ddlfiled.Items.IndexOf(ddlfiled.Items.FindByValue(""));
                          
        }

          //检查是否有图标
          public static string checkico(string val)
          {
              DataTable dte = BLL.Bllbase.get_table("[company_fav]", "cbh='" + val + "'");
              if (dte.Rows.Count > 0)
                  return "../icon/lock.png";
              else
                  return "../icon/user.png";

          }

        private void BindGrid()
        {
            DataTable table;
            
           
            table = BLL.Bllbase.get_table(tab, whe, "schbh desc", int.Parse(Grid1.PageIndex.ToString()), Grid1.PageSize);

            Grid1.DataSource = table;
            Grid1.DataBind();

        }


        //首页
        protected void btnf_Click(object sender, EventArgs e)
        {
            Grid1.PageIndex = 1;
            lblpgid.Text = "当前页:" + Grid1.PageIndex.ToString();
            BindGrid();
            txtgo.Text = Grid1.PageIndex.ToString();

        }
        //尾页
        protected void btne_Click(object sender, EventArgs e)
        {
            Grid1.PageIndex = pgCount;
            lblpgid.Text = "当前页:" + Grid1.PageIndex.ToString();
            BindGrid();
            txtgo.Text = Grid1.PageIndex.ToString();
        }

        //上一页
        protected void btnpre_Click(object sender, EventArgs e)
        {
            if (Grid1.PageIndex > 2)
                Grid1.PageIndex = Grid1.PageIndex - 1;
            else
                Grid1.PageIndex = 1;
            lblpgid.Text = "当前页:" + Grid1.PageIndex.ToString();
            BindGrid();
            txtgo.Text = Grid1.PageIndex.ToString();
        }

        //下一页
        protected void btnnext_Click(object sender, EventArgs e)
        {
            if (Grid1.PageIndex < pgCount)
                Grid1.PageIndex = Grid1.PageIndex + 1;
            else
                Grid1.PageIndex = pgCount;

            lblpgid.Text = "当前页:" + Grid1.PageIndex.ToString();

            BindGrid();
            txtgo.Text = Grid1.PageIndex.ToString();
        }

        //到指定页
        protected void btngo_Click(object sender, EventArgs e)
        {

            if (VAN.IsInt(txtgo.Text))
                Grid1.PageIndex = int.Parse(txtgo.Text);
            else
                Grid1.PageIndex = 1;

            if (int.Parse(txtgo.Text) > pgCount) Grid1.PageIndex = pgCount;

            if (int.Parse(txtgo.Text) < 1) Grid1.PageIndex = 1;

            lblpgid.Text = "当前页:" + Grid1.PageIndex.ToString();

            BindGrid();
            txtgo.Text = Grid1.PageIndex.ToString();
        }

        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
        }



        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            DataTable table;

            table = BLL.Bllbase.get_table(tab, whe, "schbh desc", int.Parse(Grid1.PageIndex.ToString()), Grid1.PageSize);   

            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);

            Grid1.DataSource = view1;
            Grid1.DataBind();
        }

 


        #region Events
        protected void btnGet_Click(object sender, EventArgs e)
        {


            if (tab != "vi_school_pub" & tab != "vi_school_del")
            {
                Alert.ShowInTop("请在公共信息或回收站使用此功能");
                return;
            }

           // int val = Grid1.PageIndex * Grid1.PageSize;

            StringBuilder sb = new StringBuilder();
            foreach (int row in Grid1.SelectedRowIndexArray)
            {
                sb.Append(Grid1.DataKeys[row][0].ToString());
                sb.Append(",");
            }

          //  Alert.ShowInTop("你选择了收藏项：" + sb.ToString().TrimEnd(','));

            //HY.BLL.Bllbase.DeleteList(tab,sb.ToString().TrimEnd(','));

            //写入收藏表
            Alert.Show(BLL.BllSales.getsales("学校", sb.ToString().TrimEnd(','), ubh, um,"SO"));

            BindGrid();

        }


        protected void Window1_Close(object sender, EventArgs e)
        {
            // Alert.ShowInTop("弹出窗口关闭了！");
            string key = ddlfiled.SelectedValue; // txt.Text.Trim();
             
              try
              {
                  if (key == "") whe = BLL.pub.pubwhere.get_where_sql(us, tab);
                  BindGrid();

              }catch(Exception ex)
              {
                //  Alert.Show("查询条件错误");
              }
              finally
              {
                  whe = ""; whe2 = "";
              }
              ViewState["whe2"] = whe2;
              ViewState["whe"] = whe;
        }

        #endregion
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ttbSearchMessage_Trigger2Click(object sender, EventArgs e)
        {
           // txt.ShowTrigger1 = true;
            string ddlf = ddlfiled.SelectedValue;

            if (ddlf == "") return;

         //  string ddlw = ddlwhere.SelectedValue;
           //string key = txt.Text.Trim();
            
            //if (ddlw.Equals("="))
            //{
            whe = " (schname like  ''%" + ddlf + "%'' or  saddress like  ''%" + ddlf + "%'') ";
            whe2 = " (schname like  '%" + ddlf + "%'  or  saddress like  '%" + ddlf + "%' )";
            //}
            //else
            //{
            //    if (ddlf == "sid" || ddlf == "编号")
            //        whe = "" + ddlf + " " + ddlw + " " + key + "";
            //    else
            //        whe = "" + ddlf + " " + ddlw + " '" + key + "'";

            //}
            //if (key == "") { whe = ""; whe2 = ""; }
            ViewState["whe2"] = whe2;
            ViewState["whe"] = whe;
            BindGrid();
        }

        /// <summary>
        /// 清除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ttbSearchMessage_Trigger1Click(object sender, EventArgs e)
        {
            //txt.Text = String.Empty;
            //txt.ShowTrigger1 = false;
            //BindGrid();
        }

        protected void btnAll_Click(object sender, EventArgs e)
        {

            ViewState["tab"] = "school";
            tab = "school";
          
            Grid1.Title = "所有信息";


            pageset();
           
            BindGrid();

        }

        protected void btnno_Click(object sender, EventArgs e)
        {
            ViewState["tab"] = "vi_school_pub";
            tab = "vi_school_pub";
           
            Grid1.Title = "公共信息";

            pageset();


            BindGrid();

        }

        protected void btnyes_Click(object sender, EventArgs e)
        {
            ViewState["tab"] = "vi_school_self";
            tab = "vi_school_self";
            Grid1.Title = "个人信息";


            pageset();


            BindGrid();
        }

        protected void btndel_Click(object sender, EventArgs e)
        {
            ViewState["tab"] = "vi_school_del";
            tab = "vi_school_del";
            Grid1.Title = "信息回收站";


            pageset();
            BindGrid();
        }

        }




    
}